<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="合同编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入合同编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同类型" prop="typeFlag">
        <el-select v-model="queryParams.typeFlag" placeholder="请选择发票状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions.contractType"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发票状态" prop="invoiceState">
        <el-select v-model="queryParams.invoiceState" placeholder="请选择发票状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions.invoiceState"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收款状态" prop="collectionState">
        <el-select v-model="queryParams.collectionState" placeholder="请选择收款状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions.collectionState"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合同状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择合同状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions.contractState"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="签订部门" prop="signDept">
        <el-input
          v-model="queryParams.signDept"
          placeholder="请输入签订部门"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="签订人" prop="signUser">
        <el-input
          v-model="queryParams.signDept"
          placeholder="请输入签订人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['contract:main:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="currentRow==null||currentRow.state!=0"
          type="primary"
          plain
          icon="el-icon-check"
          size="mini"
          @click="handleApprove"
          v-hasPermi="['contract:main:approve']"
        >确认</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['contract:main:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="open" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="contractMainList" v-loading="loading" :data="mainList" @row-click="handleRowClick" highlight-current-row>
      <el-table-column label="主键ID" align="center" prop="gid" v-if="false"/>
      <el-table-column label="合同编码" align="center" prop="code" width="230px"/>
      <el-table-column label="合同名称" align="center" prop="name" />
      <el-table-column label="客户名称" align="center" prop="customer" />
      <el-table-column label="合同类型" align="center" prop="typeFlag" width="95px" :formatter="contractTypeFormat" v-if="false"/>
      <el-table-column label="发票状态" align="center" prop="invoiceState" width="95px" :formatter="invoiceStateFormat"/>
      <el-table-column label="已开票金额" align="center" prop="invoiceAmount" width="115px"><template slot-scope="scope">{{scope.row.invoiceAmount}}元</template></el-table-column>
      <el-table-column label="发票状态" align="center" prop="collectionState" width="95px" :formatter="collectionStateFormat"/>
      <el-table-column label="已收款金额" align="center" prop="receivedAmount" width="115px"><template slot-scope="scope">{{scope.row.receivedAmount}}元</template></el-table-column>
      <el-table-column label="合同金额" align="center" prop="contractAmount" width="115px"><template slot-scope="scope">{{scope.row.contractAmount==0?'按发票结算':scope.row.contractAmount+'元'}}</template></el-table-column>
      <el-table-column label="合同状态" align="center" prop="state" width="85px" :formatter="contractStateFormat" />
      <el-table-column label="签订部门" align="center" prop="signDept" width="85px" />
      <el-table-column label="签订人" align="center" prop="signUser" width="85px" />
      <el-table-column label="备注" align="center" prop="remark" width="85px"/>
      <el-table-column label="父级合同ID" align="center" prop="parentId" v-if="false"/>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.state==0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['contract:main:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.state==0"
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['contract:main:remove']"
          >删除</el-button>
          <el-button
            v-if="scope.row.state!=0&&scope.row.invoiceState<8"
            size="mini"
            type="text"
            icon="el-icon-link"
            @click="routeRedirect('/contract/invoice',{contractId:scope.row.gid})"
            v-hasPermi="['contract:main:invoice']"
          >发票</el-button>
          <el-button
            v-if="scope.row.state!=0&&scope.row.collectionState<8"
            size="mini"
            type="text"
            icon="el-icon-coin"
            @click="routeRedirect('/contract/collection',{contractId:scope.row.gid})"
            v-hasPermi="['contract:main:invoice']"
          >收款</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-upload"
            @click="routeRedirect('/tool/attachment',{belongId:scope.row.gid})"
            v-hasPermi="['contract:main:invoice']"
          >附件</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1350px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="10">
            <el-form-item label="合同编码" prop="code" >
              <el-input v-model="form.code" placeholder="请输入合同编码" />
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="客户名称" prop="customer">
              <el-input v-model="form.customer" placeholder="请输入客户名称" type="String"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="合同标题" prop="name">
          <el-input v-model="form.name" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      <el-row>
        <el-col :span="10">
          <el-form-item label="合同金额" prop="contractAmount">
            <el-row>
              <el-col :span="12">
                <el-input v-model="form.contractAmount" :disabled="form.typeFlag==1" placeholder="请输入合同金额" type="number">
                  <template slot="append">元</template>
                </el-input>
              </el-col>
              <el-col :span="6">
                <el-checkbox v-model="form.typeFlag" v-if="open" @change="form.contractAmount=0" :checked="form.typeFlag==1" false-label=0 true-label=1>按发票结算</el-checkbox>
              </el-col>
            </el-row>

          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="发票状态" prop="invoiceState">
            <el-select v-model="form.invoiceState" placeholder="请输入发票状态" disabled>
              <el-option
                v-for="dict in stateOptions.invoiceState"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="parseInt(dict.dictValue)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="合同状态" prop="state">
            <el-select v-model="form.state" placeholder="请选择合同状态" disabled>
              <el-option
                v-for="dict in stateOptions.contractState"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="parseInt(dict.dictValue)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="签订部门" prop="signDept">
              <el-input v-model="form.signDept" placeholder="请输入签订部门" type="String"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="签订人" prop="signUser">
              <el-input v-model="form.signUser" placeholder="请输入签订人" type="String"/>
            </el-form-item>
          </el-col>

        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="父级合同ID" prop="parentId" v-if="false">
          <el-input v-model="form.parentId" placeholder="请输入父级合同ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>

      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listMain, getMain, delMain, addMain, updateMain, approveMain,exportMain } from "@/api/contract/main";
import searchSelect from "@/components/SearchSelect/index";
import {getDictByUrl} from "@/api/system/dict/data";


export default {
  name: "Main",
  components: {
    searchSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      currentRow: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同管理表格数据
      mainList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 合同状态字典
      stateOptions: [],
      selfDictData: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        typeFlag: null,
        invoiceState: null,
        collectionState: null,
        receivedAmount: null,
        contractAmount: null,
        state: null,
        signDept: null,
        signUser: null,
        parentId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "合同编码不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "合同名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  beforeCreate(){
    this.getDicts("contract_bill_state").then(response => {
      this.stateOptions.contractState = response.data;
    });
    this.getDicts("contract_bill_type").then(response => {
      this.stateOptions.contractType = response.data;
    });
    this.getDicts("invoice_state").then(response => {
      this.stateOptions.invoiceState = response.data;
    });
    this.getDicts("collection_state").then(response => {
      this.stateOptions.collectionState = response.data;
    });
  },
  created() {
    this.getList();

  },
  methods: {
    /** 查询合同管理列表 */
    getList() {
      this.currentRow = null;
      this.loading = true;
      listMain(this.queryParams).then(response => {
        this.mainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 合同状态字典翻译
    contractStateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions.contractState, row.state);
    },
    //合同类型字典翻译
    contractTypeFormat(row, column) {
      return this.selectDictLabel(this.stateOptions.contractType, row.typeFlag);
    },
    //发票状态字典翻译
    invoiceStateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions.invoiceState, row.invoiceState);
    },
    //发票状态字典翻译
    collectionStateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions.collectionState, row.collectionState);
    },
    // //点击行触发，选中或不选中复选框
    handleRowClick(row) {
      this.currentRow = row;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    resetForAdd() {
      this.form = {
        typeFlag: 0,
        invoiceState: 0,
        collectionState: 0,
        receivedAmount: 0,
        contractAmount: 0,
        state: 0
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$set(this.form,"typeFlag",0);
      this.resetForAdd();
      this.open = true;
      this.title = "添加合同管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const gid = row.gid || this.currentRow.gid
      getMain(gid).then(response => {
        this.form = response.data;
        // this.$set(this.form,"typeFlag",response.data);
        this.open = true;
        this.title = "修改合同管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gid != null) {
            updateMain(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMain(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gid = row.gid || this.currentRow.gid;
      this.$confirm('是否确认删除合同管理编号为"' + row.code + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMain(gid);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    routeRedirect(path,query) {
      this.$router.push({
        path,
        query
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有合同管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMain(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    //确认按钮操作
    handleApprove(row){
      const gid = row.gid||this.currentRow.gid;
      approveMain(gid).then(response => {
        this.msgSuccess("确认成功");
        this.open = false;
        this.getList();
      });
    }
  }
};
</script>

